我可以使用一些帮助来解决我的Shiny代码遇到的问题。我有一个带有多个选项卡的应用程序,可以进行不同类型的时间序列分析。每个分析使用相同的数据集,该数据集由用户从.csv文件加载。用户可以选择要在分析中使用的变量(列)。在每个选项卡上,我要执行以下操作:
问题在于,一组滑块会为每个选项卡上的所有表生成相同的值(应用程序中有5个用于运行分析的选项卡)。我怀疑是因为我使用的是 tagList ,但是我还不了解我在做什么错。下面的代码复制在每个选项卡上,仅反应型功能名称已更改。
#This creates sliders from the selected variables from the reactive function called
#"df_sel()" - which is just a function that allows the user to select which variables they want from the original .csv file. Note the use of "tagList". The RenderUI function below creates as
#many sliders as variables selected, and passes in the correct variable name.
#It selects the last data value from each column, since this is time series data,
#the last data value (most recent) was desired.
output$scplan <- renderUI({
vars <- df_sel()
n = nrow(vars)
tagList(lapply(colnames(vars), function(z) {
sliderInput(
sprintf("%s",z),
label = z,
min = ceiling(min(vars[[z]])), # min value is the minimum of the column
max = ceiling(max(vars[[z]])), # max is the max of the column
value = vars[[z]][[n]])
}))
#this reactive function creates a dataframe from the sliders above
#with the values the user selected, and the code
#repeats the values for as many "input$months" as were selected.
sp_numbers <- reactive({
vars <- df_sel()
num = as.integer(ncol(vars))
sp_pred <- data.frame(lapply(colnames(vars), function(z) {
input[[z]]
}))
names(sp_pred) <- colnames(vars)
n = input$sp_months
df_sp_pred = data.frame(sp_pred, z=rep(z:n,ea=NROW(input$sp_months)))
df_sp_pred[1:(length(df_sp_pred)-1)] #this removes the last column which just shows the repeat count
})
#this code renders the table of the dataframe created above.
output$spo_table <- renderTable({
sp_numbers()
})
当我将此代码放在新选项卡上时,我需要它来生成唯一值的滑块。现在,该表将填充第一个选项卡上设置的滑块。
PS-我知道习惯是提供可以运行的代码,但对我而言,这需要粘贴大量的代码;我以为这样会更容易。
此外,如果有一种更简单的方法来完成此代码中的操作,请务必进行编辑。
谢谢!