r-如何在Shiny应用程序中通过输入列名排列数据表



我正试图为一周后到期的数据可视化项目编写一个应用程序,但我似乎无法让我的应用程序提供我想要的输出。该应用程序应该接受一些输入(一个人的教育程度[这对输出没有影响]和他们拥有的三项技能(,并排列给定的数据表,使排列的表中按降序排列出最匹配的职业。

我已经向我的教授和TA寻求帮助,我的TA向我指出了arrange()函数和desc(mydata[, input$first]),但它只调用相关列中的第一个值,而不是具有该列名的实际列。

感谢您的帮助。非常感谢。我的代码在下面。

mydata <- iris
skillz <- names(mydata)
# Define UI for application
ui <- fluidPage(
# Application title
titlePanel("Weighted App"),
# Sidebar with little paragraph with inputs from person 
sidebarLayout(
sidebarPanel(
selectInput("nothing", "Hi, I am trying to get my app to work. Here are ome options" = c("GED", "Bachelor's Degree", "Master's Degree", "PhD", "Trade School Certification", "Other"), selectize = FALSE),
selectInput("first", "I want to look at these 3 traits of irises 1:", choices = skillz, multiple = FALSE),
selectInput("second", "2:", choices = skillz, multiple = FALSE),
selectInput("third", "3:", choices = skillz, multiple = FALSE)
),
# Show a table of the recommended occupations
mainPanel(
tableOutput("results")
#verbatimTextOutput('values')
#Professor:"Look at more examples on Shiny to see if you have an error. Think error in output here"
)
)
)

# Define server logic required to give weighted table
server <- function(input, output) {
output$results <- reactive({
# generate table based on inputs from the above 3 options
filtered <- arrange(mydata, desc(mydata[,input$first]), desc(mydata[,input$second]), desc(mydata[,input$third])) 
filtered
#If data table fails, maybe just print?   
#  output$values <- reactivePrint(
#   {
#   list(x1 = input$first, x2 = input$second, x3 = input$third) 
# } 
#)
})
}
# Run the application 
shinyApp(ui = ui, server = server)

同样,我认为我的错误出现在arrange()函数中,但我不确定如何使用我的输入名称实际调用列。

编辑:我尝试使用deparse(),但也只是返回"没有列'input$first'"。。。

skill1 <- deparse(input$first)
skill2 <- deparse(input$second)
skill3 <- deparse(input$third)
filtered <- arrange(mydata, desc(mydata[,skill1]), desc(mydata[,skill2]), desc(mydata[,skill3])) 
filtered

编辑2:我已经使数据更加通用。谢谢

通常,arrange和其他dplyr动词使用整洁的求值语义。这意味着,在使用包含列名称的变量时,我们需要将字符串转换为符号,然后在计算时使用!!。下面是一个简单的例子。

data(iris)
library(magrittr)
var1 <- "Species"
var2 <- "Sepal.Length"
var3 <- "Petal.Length"
iris %>% dplyr::arrange(dplyr::desc(!!rlang::sym(var1)),
dplyr::desc(!!rlang::sym(var2)), 
dplyr::desc(!!rlang::sym(var3)))

编辑1:为可复制示例添加了解决方案:

library(shiny)
library(magrittr)
ui <- fluidPage(
titlePanel("Weighted App"),
# Sidebar with little paragraph with inputs from person 
sidebarLayout(
sidebarPanel(
selectInput("education", , label = "education", 
choices = c("GED", "Bachelor's Degree", "Master's Degree", 
"PhD", "Trade School Certification", "Other"), 
selectize = FALSE),
selectInput("first", "I want to look at these 3 traits of irises 
1:", choices = skillz, multiple = FALSE),
selectInput("second", "2:", choices = skillz, multiple = FALSE),
selectInput("third", "3:", choices = skillz, multiple = FALSE)
), 
# Show a table of the recommended occupations
mainPanel(
tableOutput("results")
)
)
)

#Define server logic required to give weighted table
server <- function(input, output) {
mydata <- iris
skillz <- names(mydata)
#Use renderTable instead of reactive
output$results <- renderTable({
# Arranging the rows according to the selected variables
filtered <- mydata %>% 
dplyr::arrange(dplyr::desc(!!rlang::sym(input$first)),
dplyr::desc(!!rlang::sym(input$second)), 
dplyr::desc(!!rlang::sym(input$third))) 
unselectedCols <-setdiff(colnames(filtered), 
unique(c(input$first,
input$second,
input$third)))
#Reordering columns according to the selections
filtered <- filtered %>% 
dplyr::select(!!rlang::sym(input$first),
!!rlang::sym(input$second),
!!rlang::sym(input$third), 
!!unselectedCols)
filtered
})
}
#Run the application 
shinyApp(ui = ui, server = server)
  • 对包含要传递给排列函数的列名的变量使用sym!!运算符
  • 我添加了额外的几行代码,这些代码将使用dplyr::select对数据帧进行重新排序,以便按选择顺序显示列。类似地使用!!sym传递变量
  • 您可以直接使用renderTable在输出中显示无功表,而不是使用reactiverenderTable具有反应性上下文

最新更新