如何根据闪亮的复选框输入将多个dySeries发送到辅助轴?

我正在尝试使用lungDeaths时间序列绘制dyGraph,但如果要在“ ldeaths”旁边选择“ mdeaths”和“ fdeaths”中的至少一个,则希望将它们显示在辅助轴上。

这是一个可行的示例:

global.R

shinyUI(fluidPage(

titlePanel("Predicted Deaths from Lung Disease (UK)"),sidebarLayout(
    sidebarPanel(

        id="sidebar",width = 3,div(
            checkboxGroupInput(
                inputId = "selection",label = "Variables:",choiceNames = list("ldeaths",strong("mdeaths"),strong("fdeaths")
                                   ),choiceValues = c("ldeaths","mdeaths","fdeaths"),selected = "ldeaths"),uiOutput("rendered"),style = "font-size:75%"
        )

    ),mainPanel(
        dygraphOutput("dygraph")
    )
)
))

ui.R

shinyServer(function(input,output) {

lungDeaths <- cbind(mdeaths,fdeaths,ldeaths)

rData <- reactive({
    rData <- ts(lungDeaths[,input$selection])
})

output$dygraph <- renderDygraph({

    if(length(input$selection) > 1 & length(str_subset(input$selection,'ldeaths$'))>0){

        if(length(str_subset(input$selection,'fdeaths$'))>0 & length(str_subset(input$selection,'mdeaths$'))>0){
            dygraph(rData(),main = "Deaths from Lung Disease (UK)") %>% 
                dySeries("mdeaths",axis = 'y2') %>% 
                dySeries("fdeaths",axis = 'y2')
        }

        else if(length(str_subset(input$selection,main = "Deaths from Lung Disease (UK)") %>% 
            dySeries("mdeaths",axis = 'y2') 
        }
        else if(length(str_subset(input$selection,'fdeaths$'))>0){
            dygraph(rData(),main = "Deaths from Lung Disease (UK)") %>% 
            dySeries("fdeaths",axis = 'y2')

        }
    }
    else
        dygraph(rData(),main = "Deaths from Lung Disease (UK)")
})
})

server.R

template <typename T>
class Block_Grid
{
public:
  Block_Grid(std::size_t n,std::size_t m,std::size_t b)
  : _n(n),_m(m),_b(b),_elements(n * m)
  { 
    assert(n % b == 0);
    assert(m % b == 0);
  }

  T & operator()(std::size_t i,std::size_t j)
  {
    return _elements[index(i,j)];
  }

protected:
private:
  std::size_t index(int i,int j) const
  {
    return (i % b) 
           + (j % b) * b
           + (i / b) * b * b
           + (j / b) * n * b;
  }

  std::size_t _n;
  std::size_t _m;
  std::size_t _b;
  std::vector<T> _elements;
};

此代码可以实现我想要的功能,但是我想避免使用那么多的if和else,因为我实际上正在使用的项目有6个项目,在选择时应转到y2。有什么方法可以不必指定每种可能性吗?

我已经对how to add dySeries based on input和如何进行conditional evaluation when using pipes进行了研究,但是到目前为止我发现的答案对我不起作用。我通常会收到一条错误消息:

  

$运算符对于原子向量无效

yunmengyi 回答:如何根据闪亮的复选框输入将多个dySeries发送到辅助轴?

您应该执行以下操作:

<style>
    * {
        -webkit-box-sizing: border-box;
        -moz-box-sizing: border-box;
        box-sizing: border-box;
    }

    .PrixMin {
        float: left;
        color: #164188;
        font-size: 19px;
        margin-right: 5px;
    }

    .selecteurPrix {
        padding-top: 15px;
    }

    body {
        font-family: sans-serif;
        padding: 40px;
    }

    .valeurPrix {
        position: absolute;
        top: -59px;
        left: 177px;
    }

    .range-slider {
        position: relative;
        width: 450px;
        float: left;
        margin-right: 5px;
    }


    @media (-webkit-min-device-pixel-ratio:0) {
        /* Non-IE styles here*/
        .range-slider .input-range {
            -webkit-appearance: none;
            width: 449px;
            height: 5px;
            border-radius: 5px;
            background: #ccc;
            outline: none;
        }
    }

    @media (-ms-high-contrast:none),(-ms-high-contrast:active) {
        /* IE styles here*/
        .range-slider .input-range {
            /*removes default webkit styles*/
            -webkit-appearance: none;
            /*fix for FF unable to apply focus style bug */
            border: 1px solid white;
            /*required for proper track sizing in FF*/
            width: 450px;
            outline: none;
        }
    }

    .range-slider .input-range::-webkit-slider-thumb {
        -webkit-appearance: none;
        width: 20px;
        height: 20px;
        border-radius: 50%;
        background: #164188;
        cursor: pointer;
        -webkit-transition: background .15s ease-in-out;
        transition: background .15s ease-in-out;
    }

                .range-slider .input-range::-webkit-slider-thumb:hover {
                    background: #164188;
                }

            .range-slider .input-range:active::-webkit-slider-thumb {
                background: #164188;
            }

            .range-slider .input-range::-moz-range-thumb {
                width: 20px;
                height: 20px;
                border: 0;
                border-radius: 50%;
                background: #164188;
                cursor: pointer;
                -webkit-transition: background .15s ease-in-out;
                transition: background .15s ease-in-out;
            }

                .range-slider .input-range::-moz-range-thumb:hover {
                    background: #164188;
                }

            .range-slider .input-range:active::-moz-range-thumb {
                background: #164188;
            }

        .range-slider .range-value {
            display: inline-block;
            position: relative;
            width: 100px;
            color: #fff;
            font-size: 23px;
            line-height: 32px;
            text-align: center;
            border-radius: 3px;
            background: #164188;
            padding: 5px 10px;
            margin-left: 7px;
        }
    ::-moz-range-track {
        background: #ccc;
        border: 0;
    }
    input::-moz-focus-inner {
        border: 0;
    }

    input[type=range] {
        margin: 2px;
    }

        input[type=range]::-ms-track {
            width: 450px;
            height: 5px;
            /*remove bg colour from the track,we'll use ms-fill-lower and ms-fill-upper instead */
            background: transparent;
            /*leave room for the larger thumb to overflow with a transparent border */
            border-color: transparent;
            border-width: 6px 0;
            /*remove default tick marks*/
            color: transparent;
        }

        input[type=range]::-ms-fill-lower {
            background: #ccc;
            border-radius: 10px;
        }

        input[type=range]::-ms-fill-upper {
            background: #ccc;
            border-radius: 10px;
        }

        input[type=range]::-ms-thumb {
            border: none;
            height: 16px;
            width: 16px;
            border-radius: 50%;
            background: #164188;
        }

        input[type=range]:focus::-ms-fill-lower {
            background: #ccc;
        }

        input[type=range]:focus::-ms-fill-upper {
            background: #ccc;
        }
</style>
,

我成功了,所以我发布了ansewr来帮助一直困扰同一问题的人...

在尝试进行条件管道评估时,我没有正确使用if else,这就是为什么我不断收到问题中提到的错误的原因。

事实证明,我必须使用点(。)来指定要在以下函数中插入管道之前的对象的确切位置,这可能是因为该函数位于条件评估中。下面的代码正是我想要的:

 output$dygraph <- renderDygraph({

        dygraph(rData(),main = "Deaths from Lung Disease (UK)") %>% 
            {
                if(length(str_subset(input$selection,'mdeaths$')) > 0 )
                    dySeries(.,"mdeaths",axis = 'y2')
                else
                    .
            } %>% 
            {
                if(length(str_subset(input$selection,'fdeaths$')) > 0 )
                    dySeries(.,"fdeaths",axis = 'y2')
                else
                    .
            }

})

现在,无论何时将其选择为输入,该dygraph都会在副轴上显示该dyseries

本文链接:https://www.f2er.com/3158991.html

大家都在问