通过修改另一个rhandsontable来修改rhandsontable
Modify rhandsontable by modifying another rhandsontable
我有以下数据框:
DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
transmission=factor(rep(c("automatic","manual"),5)))
我创建了一个闪亮的应用程序,其目标是根据第一个 rhandsontable.More 的修改来修改第二个 rhandsontable 具体来说:
1.Initially 将此 DF2
的第一行显示为 rhandsontable。
2.Then 我通过 rhandsontable 选择一个下拉选项来修改第一行第一列的单元格。
3.I通过jsonlite将修改后的rhandsontable转换为dataframe。
4.Then 我从该数据框中提取修改后的值,并对初始 DF2
第一列进行子集化。
5.Then 我显示了第二个 rhandsontable 的第一行,它应该显示修改后的数据集,但它没有。
#ui.r
library(shiny)
library(rhandsontable)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
rHandsontableOutput("hot"),
rHandsontableOutput("hot2")
)
)
)
#server.r
library(shiny)
library(rhandsontable)
library(jsonlite)
server <- function(input, output) {
#Create rhandsontable as a reactive expression
DFR2<-reactive({
rhandsontable(DF2[1,1:2], rowHeaders = NULL,width=1400,height = 200)%>%
hot_col(colnames(DF2)[1])
})
#Display the rhandsontable
output$hot <- renderRHandsontable({
DFR2()
})
#Convert the rhandsontable to a daraframe
DFR3<-reactive({
data_fram <- fromJSON(DFR2()$x$data)
})
#Subset the initial dataframe by value of the 1st row-1st column cell of DF3
DFR4<-reactive({
newdata <- DF2[ which(DF2[,1]==DFR3()[1,1]), ]
})
#Display the new rhandsontable
output$hot2 <- renderRHandsontable({
rhandsontable(DFR4()[1,], rowHeaders = NULL,width=1400,height = 200)%>%
hot_col(colnames(DFR4()))
})
}
主要的解决方法是在DFR3
中你应该引用input$hot
而不是DFR2()
:
DFR3 <- reactive({
req(input$hot)
hot_to_r(input$hot)
})
使用 %in%
运算符作为 hot_to_r
returns 无法与使用 ==
运算符的正常因子进行比较的有序因子。 (或者将 DF2$agency_postcode
定义为有序因子)
DFR4 <- reactive({
req(DFR3())
DF2[ which(DF2[,1] %in% DFR3()[1, 1]), ]
})
输出:
我有以下数据框:
DF2 = data.frame(agency_postcode = factor(rep(c(12345,45678,24124,32525,32325),2)),
car_group=factor(rep(c("Microcar","City car","Supermini","Compact","SUV"),2)),
transmission=factor(rep(c("automatic","manual"),5)))
我创建了一个闪亮的应用程序,其目标是根据第一个 rhandsontable.More 的修改来修改第二个 rhandsontable 具体来说:
1.Initially 将此 DF2
的第一行显示为 rhandsontable。
2.Then 我通过 rhandsontable 选择一个下拉选项来修改第一行第一列的单元格。
3.I通过jsonlite将修改后的rhandsontable转换为dataframe。
4.Then 我从该数据框中提取修改后的值,并对初始 DF2
第一列进行子集化。
5.Then 我显示了第二个 rhandsontable 的第一行,它应该显示修改后的数据集,但它没有。
#ui.r
library(shiny)
library(rhandsontable)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
),
mainPanel(
rHandsontableOutput("hot"),
rHandsontableOutput("hot2")
)
)
)
#server.r
library(shiny)
library(rhandsontable)
library(jsonlite)
server <- function(input, output) {
#Create rhandsontable as a reactive expression
DFR2<-reactive({
rhandsontable(DF2[1,1:2], rowHeaders = NULL,width=1400,height = 200)%>%
hot_col(colnames(DF2)[1])
})
#Display the rhandsontable
output$hot <- renderRHandsontable({
DFR2()
})
#Convert the rhandsontable to a daraframe
DFR3<-reactive({
data_fram <- fromJSON(DFR2()$x$data)
})
#Subset the initial dataframe by value of the 1st row-1st column cell of DF3
DFR4<-reactive({
newdata <- DF2[ which(DF2[,1]==DFR3()[1,1]), ]
})
#Display the new rhandsontable
output$hot2 <- renderRHandsontable({
rhandsontable(DFR4()[1,], rowHeaders = NULL,width=1400,height = 200)%>%
hot_col(colnames(DFR4()))
})
}
主要的解决方法是在DFR3
中你应该引用input$hot
而不是DFR2()
:
DFR3 <- reactive({
req(input$hot)
hot_to_r(input$hot)
})
使用 %in%
运算符作为 hot_to_r
returns 无法与使用 ==
运算符的正常因子进行比较的有序因子。 (或者将 DF2$agency_postcode
定义为有序因子)
DFR4 <- reactive({
req(DFR3())
DF2[ which(DF2[,1] %in% DFR3()[1, 1]), ]
})